﻿using Common.Communication;
using Common.Device.Enums;
using Common.Device.Interfaces;
using Common.Device.ViewModels;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.Device.Models
{
    public class DianYaCaiJi : Device
    {/// <summary>
     /// 测试模式
     /// </summary>
        public Enums.DianYaCaiJiEnum.TestMode mode { get; set; }
        public string 通道 { get; set; }
        public DeviceEnum.DeviceModelEnum 设备型号 { get; set; }
        public List<Enums.DianYaCaiJiEnum.Channel> channels { get; set; }
        public string unity { get; set; }
        
        
        /// <summary>
        /// 生成测试指令
        /// </summary>
        /// <param name="deviceName">仪器名称</param>
        /// <returns></returns>
        public override Order GetOrder(string deviceName)
        {
            //测试指令
            Order order = new Order();
            order.temp = mode.ToString();
            order.name = deviceName;
            switch (mode)
            {
                case DianYaCaiJiEnum.TestMode.电压采集:
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道, channels , 设备型号 });
                    break;
                default:
                    break;
            }
            return order;
        }
        /// <summary>
        /// 测试并返回测试结果
        /// </summary>
        /// <param name="dev">仪器实体</param>
        /// <param name="testOrder">测试指令</param>
        /// <returns></returns>
        public override DeviceResult GetResult(object dev, TestOrder testOrder)
        {
            //仪器实体
            var device = dev as IDianYaCaiJi;
            //测试结果实体
            var result = new DeviceResult();
            Result<List<DianYaCaiJiEnum.Channel>> r1 = new Result<List<DianYaCaiJiEnum.Channel>>();
            int len = 0;
            int datalen = 0;
            int vol = 0;
            switch (设备型号)
            {
                //case "ZH-4211-14D2 (30V)": datalen = 7; len = 2; vol = 30; break;
                //case "ZH-44081-14M2 (10V,20V)": datalen = 21; len = 2; vol = 10; break;
                //case "ZH-42141-14N2 (10V)": datalen = 33; len = 2; vol = 10; break;
                //case "ZH-42241-14N2 (5V)": datalen = 101; len = 2; vol = 5; break;
                //case "ZH-42241-14N2 (30V)": datalen = 101; len = 2; vol = 30; break;
                //case "ZH-44121-14F2 (10V,20V)": datalen = 29; len = 2; vol = 10; break;
                //case "ZH-44241-14F2 (30V)": datalen = 101; len = 2; vol = 30; break;
                //case "ZH-42241-14F2 (10V)": datalen = 101; len = 2; vol = 10; break;
                case DeviceEnum.DeviceModelEnum.ZH_4211_14D2_30V: datalen = 7; len = 2; vol = 30; break;
                case DeviceEnum.DeviceModelEnum.ZH_44081_14M2_10V_20V: datalen = 21; len = 2; vol = 10; break;
                case DeviceEnum.DeviceModelEnum.ZH_42141_14N2_10V: datalen = 33; len = 2; vol = 10; break;
                case DeviceEnum.DeviceModelEnum.ZH_42241_14N2_5V: datalen = 101; len = 2; vol = 5; break;
                case DeviceEnum.DeviceModelEnum.ZH_42241_14N2_30V: datalen = 101; len = 2; vol = 30; break;
                case DeviceEnum.DeviceModelEnum.ZH_44121_14F2_10V_20V: datalen = 29; len = 2; vol = 10; break;
                case DeviceEnum.DeviceModelEnum.ZH_44241_14F2_30V: datalen = 101; len = 2; vol = 30; break;
                case DeviceEnum.DeviceModelEnum.ZH_42241_14F2_10V: datalen = 101; len = 2; vol = 10; break;
                default:
                    break;
            }
            //测试模式判断
            switch (mode)
            {
                case DianYaCaiJiEnum.TestMode.电压采集:
                    r1 = device.GetValue(设备型号, channels);
                    //测试结果实体赋值
                    result.success = r1.IsSucceed; if (!result.success)
                    {
                        result.result = r1.Err;
                        break;
                    }

                    foreach (var item in r1.Value)
                    {
                        if (item.value > item.max || item.value < item.min)
                        {
                            result.success = false;
                            result.result = result.result+$"{item.channel}号异常，{item.min}~{item.max}实际{item.value}";
                        }
                        else
                        {
                            result.result = result.result+$"{item.channel}号通过，{item.min}~{item.max}实际{item.value}";
                        }
                    }
                    //result.value = r1.Value;
                    //result.unit = 通道 == "湿度" ? "%RH" : "℃";
                    //result.result = DataConvert.ToUnit(r1.Value, "F3") + result.unit;
                    break;
                default:
                    break;
            }
            return result;
        }
    }
}
